/*
 * COX1215.c
 *
 *  Created on: 2012/10/23
 *      Author: shenming
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rule_info.h"
#include "core.h"
#include "rule_lib.h"
#include "trace.h"
#include "TraceAst.h"

static const PSZ RuleID = "COX1215";

static const PSZ MESSAGE_ENG = "";
static const PSZ MESSAGE_JPN = "式中に0を含まず、演算が0となっています";
static const PSZ MESSAGE_CHS = "";

static const U2 RANK = MSG_RANK_ERR;

static PRULEOUT pRuleHead;
static PRULEOUT pRuleTail;

/******************************************************************
 * static function:
 * isZeroVal
 *******************************************************************/
static BOOL isZeroVal(AST valueInfo) {
	BOOL flag = FALSE;
	DOUBLE dbVal;
	INTVALUE intVal;
	if (CValueInfo.isFloating(valueInfo)) {
		dbVal = CValueInfo.getFloatingValue(valueInfo);
		if (dbVal == 0) {
			flag = TRUE;
		}
	} else {
		intVal = CValueInfo.getIntegerValue(valueInfo);
		if (intVal.u8Remainder == 0) {
			flag = TRUE;
		}
	}
	return flag;
}

/******************************************************************
 * static function:
 * hasZeroValInExp
 *******************************************************************/
static BOOL hasZeroValInExp(AST exp) {
	AST leftExp = CExpression.getLeftNode(exp);
	AST rightExp = CExpression.getRightNode(exp);
	AST valueInfo = 0;
	BOOL bRe = FALSE;
	if (leftExp > 0) {
		if (CExpressionOpKind.isConstantNumber(
				CExpression.getOpKind(leftExp))) {
			valueInfo = CExpression.getPrimaryConstValue(leftExp);
			bRe = isZeroVal(valueInfo);
		} else {
			bRe = hasZeroValInExp(leftExp);
		}
	}
	if(bRe){
		return bRe;
	}
	if (rightExp > 0) {
		if (CExpressionOpKind.isConstantNumber(
				CExpression.getOpKind(rightExp))) {
			valueInfo = CExpression.getPrimaryConstValue(rightExp);
			bRe = isZeroVal(valueInfo);
		} else {
			bRe = hasZeroValInExp(rightExp);
		}
	}
	return bRe;
}

/******************************************************************
 * static function:
 * checkNG
 *******************************************************************/
static INT checkNG(AST exp) {
	INT result = RULE_RESULT_OK;
	AST valueInfo = 0;

	valueInfo = CExpression.getCalcuteValue(exp);
	if (!isZeroVal(valueInfo)) {
		return RULE_RESULT_OK;
	}

	if (!hasZeroValInExp(exp)) {
		return RULE_RESULT_NG;
	}

	return result;
}

/******************************************************************
 * main logic
 *******************************************************************/
static S4 RuleMain(U2 kind, AST index, U4 event) {

	INT result = RULE_RESULT_OK;
	PRULEOUT newNode = NULL;
	PRULEOUT tmpNode = NULL;
	PRULEOUT tmpNode1 = NULL;

	AST codeScp = 0;
	AST expInfo = 0;

	if (kind != TR_KIND_DECLARATION) {
		return EXIT_SUCCESS;
	}

	if (!(event & EVT_DECLARATION)) {
		return EXIT_SUCCESS;
	}

	if (!isConstInitExp(index, &expInfo)) {
		return EXIT_SUCCESS;
	}

	if (!isDnInitExp(expInfo)) {
		return EXIT_SUCCESS;
	}

	result = checkNG(expInfo);

	/*re-checked?*/
	tmpNode = pRuleHead;
	while (tmpNode != NULL ) {
		if (tmpNode->indexInfo == index) {
			if (result > RULE_RESULT_OK) {
				result = RULE_RESULT_OK;
			} else {
				if (tmpNode == pRuleHead) {
					pRuleHead = pRuleHead->next;
					free(tmpNode);
				} else if (tmpNode == pRuleTail) {
					tmpNode1->next = NULL;
					pRuleTail = tmpNode1;
					free(tmpNode);
				} else {
					tmpNode1->next = tmpNode->next;
					free(tmpNode);
				}
			}
			break;
		}
		tmpNode1 = tmpNode;
		tmpNode = tmpNode->next;
	}

	/*make result struct*/
	if (result > RULE_RESULT_OK) {
		newNode = (PRULEOUT) malloc(sizeof(RULEOUT));
		if (pRuleHead == NULL ) {
			pRuleHead = newNode;
		}

		codeScp = CExpression.getCodeScope(expInfo);
		newNode->indexInfo = index;
		newNode->fileIndex = CCodeScope.getFileIndex(codeScp);
		newNode->beginLine = CCodeScope.getBeginLine(codeScp);
		newNode->beginCol = CCodeScope.getBeginLineOffset(codeScp);
		newNode->endLine = CCodeScope.getEndLine(codeScp);
		newNode->endCol = CCodeScope.getEndLineOffset(codeScp);
		newNode->ruleID = RuleID;
		newNode->errRank = RANK;

		sprintf(newNode->msgBuf_eng, MESSAGE_ENG);
		sprintf(newNode->msgBuf_chs, MESSAGE_CHS);
		sprintf(newNode->msgBuf_jpn, MESSAGE_JPN);

		newNode->rfileIndex = 0;
		newNode->rlocLine = 0;
		newNode->next = NULL;

		if (pRuleTail != NULL ) {
			pRuleTail->next = newNode;
		}

		pRuleTail = newNode;
	}

	return EXIT_SUCCESS;
}

VOID COX1215_register(VOID) {
	PRULEMAN * pCurRule = &stRuleManager;
	PRULEMAN newRuleMan = (PRULEMAN) malloc(sizeof(RULEMAN));
	memset(newRuleMan, 0, sizeof(RULEMAN));
	newRuleMan->ruleID = RuleID;
	newRuleMan->ruleSwitch = OFF;
	newRuleMan->headOut = &pRuleHead;
	newRuleMan->tailOut = &pRuleTail;
	newRuleMan->ruleMain_cmd = NULL;
	newRuleMan->ruleMain_ast = RuleMain;
	newRuleMan->nextRule = NULL;
	if (stRuleManager == NULL ) {
		stRuleManager = newRuleMan;
	} else {
		while ((*pCurRule)->nextRule != NULL ) {
			pCurRule = &((*pCurRule)->nextRule);
		}
		(*pCurRule)->nextRule = newRuleMan;
	}
}

